package com.aitou.mrylive.presentation.ui.main.me.transaction;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.TextView;

import com.jakewharton.rxbinding.view.RxView;
import com.aitou.mrylive.R;
import com.aitou.mrylive.data.bean.BaseResponse;
import com.aitou.mrylive.data.bean.IncomeBean;
import com.aitou.mrylive.domain.TransactionManager;
import com.aitou.mrylive.presentation.ui.base.BaseActivity;
import com.aitou.mrylive.presentation.ui.base.BaseObserver;
import com.aitou.mrylive.presentation.ui.withdraw.PresentRecordActivity;
import com.aitou.mrylive.presentation.ui.withdraw.WithDrawNumActivity;
import com.aitou.mrylive.util.Const;
import com.umeng.analytics.MobclickAgent;

import java.util.concurrent.TimeUnit;

import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * @author Muyangmin
 * @since 1.0.0
 * 收益提现
 */
public class IncomeActivity extends BaseActivity {
    private static final String KEY_AVATER="avater";
    private TextView tvTotalIncome, tvWithdrawRecord, tvTroubleShot;
    private TextView tvWithdrawLimit;
    private TextView btnWithdraw;
    private String rmb = "0";
    private IncomeBean mIncomeBean;
    private String avater ;

    public static Intent createIntent(Context context,String avater) {
        Intent i = new Intent(context, IncomeActivity.class);
        i.putExtra(KEY_AVATER,avater);
        return  i;
    }

    @Override
    protected void onResume() {
//        MobclickAgent.onPageStart("收益提现");
        super.onResume();
        new TransactionManager().getIncomeBean()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse<IncomeBean>>(this) {
                    @Override
                    public void onSuccess(BaseResponse<IncomeBean> response) {
                        showIncome(response.getData());
                    }
                });
    }

    @Override
    protected void onPause() {
//        MobclickAgent.onPageEnd("收益提现");
        super.onPause();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_income;
    }

    @Override
    protected void findViews(Bundle savedInstanceState) {
        tvTotalIncome = $(R.id.income_tv_total_income);
        tvTroubleShot = $(R.id.income_tv_troubleshot);
        tvWithdrawLimit = $(R.id.income_tv_withdraw_limit);
        tvWithdrawRecord = $(R.id.tv_toolbar_right);
        btnWithdraw = $(R.id.income_btn_withdrawal);

    }

    @Override
    protected void init() {
        avater = getIntent().getStringExtra(KEY_AVATER);
        TextView tvCurrencyLabel = $(R.id.income_tv_total_label);
        tvCurrencyLabel.setText(getString(R.string.income_total, getString(R.string.app_currencyearn)));

//        subscribeFeatureStub(btnWithdraw);
//        subscribeFeatureStub(tvWithdrawRecord);
        subscribeFeatureStub(tvTroubleShot);
        RxView.clicks(btnWithdraw).throttleFirst(Const.VIEW_THROTTLE_TIME,
                TimeUnit.MILLISECONDS)
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        if(rmb.equals("0")){
                            toastShort(getString(R.string.charge_withdraw_no_enough_money,"1"));
                        }else {
                            startActivity(WithDrawNumActivity.createIntent(IncomeActivity.this,
                                    avater, mIncomeBean));
                        }
                    }
                });
        RxView.clicks(tvWithdrawRecord).throttleFirst(Const.VIEW_THROTTLE_TIME,
                TimeUnit.MILLISECONDS)
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        startActivity(PresentRecordActivity.createIntent(IncomeActivity.this));
                    }
                });

    }

    private void showIncome(IncomeBean bean){
        mIncomeBean = bean;
        rmb = TextUtils.isEmpty(bean.getRmb())? "0" :bean.getRmb();
        tvTotalIncome.setText(String.valueOf(bean.getEarnbean()));
        tvWithdrawLimit.setText(rmb);
    }


}
